import com.alibaba.fastjson.JSON;
import com.qian.api.ddj.util.SignUtils;
import com.yaowk.api.utils.HttpUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.junit.Test;

import java.util.Map;
import java.util.TreeMap;

public class PayTest03 {

    static String signKey = "4b2ae0fe87b74692add6aae52ed6cce5";
    private static String partner = "1000164624";

    @Test
    public void pay() {

        String pid = partner;
        String gateWay = "http://120.77.22.14:8380/pay/gateway/unify.do";//请求
        Map<String, String> requestMap = new TreeMap<String, String>();
        requestMap.put("TRDE_CODE", "20001");//交易码
        requestMap.put("PRT_CODE", pid);//机构号
        requestMap.put("VER_NO", "1.0");//版本号  1.0

        requestMap.put("MERC_ID", "834316047330001");//进件成功后返回的商户号
        requestMap.put("BIZ_CODE", "5003");//微信1003 支付宝2003QQ扫码5003
        requestMap.put("ORDER_NO", "cs" + System.currentTimeMillis() + System.currentTimeMillis());//订单号  确保接入系统唯一

        requestMap.put("TXN_AMT", "3");//订单金额
        requestMap.put("PRO_DESC", "shop");//商品描述

        requestMap.put("NOTIFY_URL", "http://notify.url");//异步通知地址
        String nonce_str = System.currentTimeMillis() + "";// 生成随机字符串
        requestMap.put("NON_STR", nonce_str);//随机字符串
        requestMap.put("TM_SMP", System.currentTimeMillis() + "");//时间戳
        requestMap.put("SIGN_TYP", "MD5");//签名方式

        requestMap.put("SIGN_DAT", DigestUtils.md5Hex(SignUtils.params(requestMap) + "&KEY=" + signKey));
        String body = JSON.toJSONString(requestMap);
        System.out.println(body);
        String receive = HttpUtils.post(gateWay, body);
        System.out.println(receive);
//        {"RETURNCODE":"0001","MERC_ID":"834584047330002","QR_CODE":"","PRT_CODE":"1000158419","RETURNCON":"通道已关闭","RET_HTML":"","ORDER_NO":"cs1514189304933","SIGN_DAT":"2feac18f14c837c8e9dff2e8dc40ff61","OUT_TRADE_NO":""}
//{"RETURNCODE":"0000","MERC_ID":"834316047330001","QR_CODE":"weixin://wxpay/bizpayurl?pr=M6lydxv","PRT_CODE":"1000164624","RETURNCON":"SUCCESS","RET_HTML":"","ORDER_NO":"cs1514191267585","SIGN_DAT":"df20760aeb1d99b6021379b3f6fdd7e7","OUT_TRADE_NO":"201712250118624360"}

    }

    @Test
    public void query() {
        String pid = partner;
        String gateWay = "http://120.77.22.14:8380/pay/gateway/unify.do";//请求
        Map<String, String> requestMap = new TreeMap<String, String>();
        requestMap.put("TRDE_CODE", "20005");//交易码
        requestMap.put("PRT_CODE", pid);//机构号
        requestMap.put("VER_NO", "1.0");//版本号  1.0

        requestMap.put("MERC_ID", "834316047330002");//进件成功后返回的商户号
        requestMap.put("NON_STR", System.currentTimeMillis() + "");//随机字符串
        requestMap.put("TM_SMP", System.currentTimeMillis() + "");//时间戳
        requestMap.put("SIGN_TYP", "MD5");//签名方式

        requestMap.put("SIGN_DAT", DigestUtils.md5Hex(SignUtils.params(requestMap) + "&KEY=" + signKey));
        String body = JSON.toJSONString(requestMap);
        System.out.println(body);
        String receive = HttpUtils.post(gateWay, body);
        System.out.println(receive);
    }

    @Test
    public void wd() {
        String pid = partner;
        String gateWay = "http://120.77.22.14:8380/pay/gateway/withdraw.do";//请求
        Map<String, String> requestMap = new TreeMap<String, String>();
        requestMap.put("TRDE_CODE", "20003");//交易码
        requestMap.put("PRT_CODE", pid);//机构号
        requestMap.put("VER_NO", "1.0");//版本号  1.0

        requestMap.put("MERC_ID", "834316047330001");//商户号
        requestMap.put("OUT_WITHDRAW_NO", "csdf" + System.currentTimeMillis());//订单号  确保接入系统唯一
        requestMap.put("WITHDRAW_MONEY", "5");//代付金额，单位为元

        requestMap.put("PERSON_NM", "收款人姓名");//收款人姓名
        requestMap.put("CARD_NO", "收款人卡号");//收款人卡号
        requestMap.put("BNK_NM", "开户行");//收款人银行卡开户行
        requestMap.put("BNK_NO", "银行代号");//银行代号 例CMB
        requestMap.put("BNK_CD", "308551024043");//联行号
        requestMap.put("CRP_ID_NO", "身份证号");//收款人身份证号
        requestMap.put("PHONE_NO", "预留手机号");//收款人手机号
        requestMap.put("PAY_ID", "D0");//代付类型  D0额度代付T1普通代付

        String nonce_str = System.currentTimeMillis() + "";// 生成随机字符串
        requestMap.put("NON_STR", nonce_str);//随机字符串
        requestMap.put("TM_SMP", System.currentTimeMillis() + "");//时间戳
        requestMap.put("SIGN_TYP", "MD5");//签名方式
        String sign = DigestUtils.md5Hex(SignUtils.params(requestMap) + "&KEY=" + signKey);
        requestMap.put("SIGN_DAT", sign);//签名数据

        String json = JSON.toJSONString(requestMap);
        String receive = HttpUtils.post(gateWay, json);
        System.out.println(receive);
    }
}
